/**
 * 字符权限校验
 * @param value Array
 * @returns boolean
 */
export function checkPermi(permissions: any[], value: any[]): boolean {
  if (value && value instanceof Array && value.length > 0) {
    const permissionDatas = value
    const all_permission = "*:*:*";

    const hasPermission = permissions.some(permission => {
      return all_permission === permission || permissionDatas.includes(permission)
    })

    if (!hasPermission) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like checkPermi="['system:user:add','system:user:edit']"`)
    return false
  }
}

/**
 * 角色权限校验
 * @param store 
 * @param value 
 * @returns boolean
 */
export function checkRole(roles: any[], value: any[]): boolean {
  if (value && value instanceof Array && value.length > 0) {
    const permissionRoles = value
    const super_admin = "admin";
    const hasRole = roles.some(role => {
      return super_admin === role || permissionRoles.includes(role)
    })
    if (!hasRole) {
      return false
    }
    return true
  } else {
    console.error(`need roles! Like checkRole="['admin','editor']"`)
    return false
  }
}

/**
 * 校验权限
 * @param permissions 
 * @param permission 
 * @returns 
 */
export function authPermission(permissions: any[], permission: string): boolean {
  const all_permission = "*:*:*";
  if (permission && permission.length > 0) {
    return permissions.some(v => {
      return all_permission === v || v === permission
    })
  } else {
    return false
  }
}

/**
 * 校验角色
 * @param roles 
 * @param role 
 * @returns 
 */
export function authRole(roles: any[], role: string): boolean {
  const super_admin = "admin";
  if (role && role.length > 0) {
    return roles.some(v => {
      return super_admin === v || v === role
    })
  } else {
    return false
  }
}

// 验证用户是否具备某权限
export function hasPermi(permissions: any[], permission: string): boolean {
  return authPermission(permissions, permission);
}

// 验证用户是否含有指定权限，只需包含其中一个
export function hasPermiOr(hasPermissions: any[], permissions: any[]): boolean {
  return permissions.some(item => {
    return authPermission(hasPermissions, item)
  })
}

// 验证用户是否含有指定权限，必须全部拥有
export function hasPermiAnd(hasPermissions: any[], permissions: any[]): boolean {
  return permissions.every(item => {
    return authPermission(hasPermissions, item)
  })
}

// 验证用户是否具备某角色
export function hasRole(roles: any[], role): boolean {
  return authRole(roles, role);
}

// 验证用户是否含有指定角色，只需包含其中一个
export function hasRoleOr(hasRoles: any[], roles: any[]): boolean {
  return roles.some(item => {
    return authRole(hasRoles, item)
  })
}
// 验证用户是否含有指定角色，必须全部拥有
export function hasRoleAnd(hasRoles: any[], roles: any[]): boolean {
  return roles.every(item => {
    return authRole(hasRoles, item)
  })
}